package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.domain.entity.Employee;
import com.example.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R getEmployeeDetails(HttpServletRequest request, @RequestBody Employee employee) {
        String username = employee.getUsername();
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(username), Employee::getUsername, username);
        Employee one = employeeService.getOne(wrapper);

        if (one == null) {
            return R.error("用户不存在");
        } else if (!one.getPassword().equals(password)) {
            return R.error("密码错误");
        } else if (one.getStatus()==0) {
            return R.error("该账户已被禁用");
        }

        // 登陆成功 将id存入session中
        request.getSession().setAttribute("employee", one.getId());
        return R.success(one);
    }


    @PostMapping("/logout")
    public R logout(HttpServletRequest request) {
        // 登陆成功 将id存入session中
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    @GetMapping("/page")
    public R page(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize, @RequestParam(value = "name", defaultValue = "") String name) {
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(name), Employee::getName, name);
        IPage<Employee> employeePage = new Page<>(page, pageSize);
        IPage<Employee> iPage = employeeService.page(employeePage, wrapper);
        return R.success(iPage);
    }

    @PostMapping
    public R add(@RequestBody Employee employee, HttpServletRequest request) {
        return employeeService.saveEmployee(employee,request);
    }

    @GetMapping("/{id}")
    public R find(@PathVariable Long id) {
        Employee employee = employeeService.getById(id);
        if (employee != null) {
            return R.success(employee);
        }
        return R.error("该用户不存在");
    }

    @PutMapping
    public R update(@RequestBody Employee employee,HttpServletRequest request) {
        Long id = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateUser(id);
        employee.setUpdateTime(LocalDateTime.now().toString());
        employeeService.updateById(employee);
        return R.success("修改成功");
    }


    // @PutMapping
    // public R edit(@RequestBody Employee employee,HttpServletRequest request) {
    //     Long id = (Long) request.getSession().getAttribute("employee");
    //     employee.setUpdateUser(id);
    //     return R.success(employeeService.saveOrUpdate(employee));
    // }
}
